package com.common.app.dialog

import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ScrollView
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.fragment.app.FragmentManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.common.app.R
import com.common.app.data.bean.DialogButBean
import com.common.app.databinding.BaseCommonDialogBinding
import com.common.base.app.fragment.BaseDialogFragment
import com.common.base.app.extras.*
import com.common.base.utils.AppUtils
import com.common.base.utils.LayoutManagerUtil
import com.common.base.utils.ViewUtils
import com.common.base.widget.round.RoundFrameLayout

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/9/1
 * <p>
 * 描述：普通对话框
 * <p>
 * 修订历史：
 */
class CommonDialog : BaseDialogFragment<BaseCommonDialogBinding>() {

    companion object {
        fun newInstance(): CommonDialog {
            return CommonDialog()
        }
    }

    //内容文本
    private var contentTxt: CharSequence? = null

    //内容文本颜色
    private var contentColor = R.color.color_323232

    //内容区域是否居中
    private var contentCenter: Boolean = true

    //弹出位置是否居中
    private var positionIsCenter: Boolean = true

    //内容区域填充View布局
    private var contentLayout: View? = null

    //标题
    private var titleTxt = ""

    //标题颜色
    private var titleTxtColor = R.color.color_323232

    //标题背景颜色
    private var titleBackgroundColor = R.color.white

    //底部按钮的背景颜色
    private var bottomButtonBackground = R.color.transparent

    //按钮点击弹框是否默认消失
    private var defDismiss: Boolean = true

    //不需要底部的按钮
    private var noBottomButtons: Boolean = false

    //是否显示标题右边的关闭按钮
    private var showDialogClose: Boolean = false

    //按钮列表数据
    private val mData = ArrayList<DialogButBean>()

    //按钮列表适配器
    private val mAdapter = CommonDialogButtonAdapter(this)

    //主动设置对话框的高度
    private var dialogHeight = 0

    //主动设置对话框的宽度
    private var dialogWidth = ViewGroup.LayoutParams.MATCH_PARENT

    //底部边距
    private var dialogMarginBottom = 0

    override fun getViewBinding() = BaseCommonDialogBinding.inflate(layoutInflater)

    override fun getSelfHeight(): Int = (dialogHeight == 0).yes { ViewGroup.LayoutParams.WRAP_CONTENT }.otherwise { dialogHeight }

    override fun getSelfWidth(): Int =
            positionIsCenter.yes { dp2px(320) }.otherwise { dialogWidth }

    override fun initView(view: View) {
        mViewBinding.dialogContentLayout.removeAllViews()
        positionIsCenter.yes { setShowPositionCenter() }.otherwise {
            val delegate = mViewBinding.dialogRootLayout.getDelegate()
            delegate.setCornerRadius_BL(0)
            delegate.setCornerRadius_BR(0)
            delegate.setCornerRadius_TL(dp2px(8))
            delegate.setCornerRadius_TR(dp2px(8))
        }
        (dialogMarginBottom != 0).yes {
            val params = mViewBinding.dialogRootLayout.layoutParams as FrameLayout.LayoutParams
            params.bottomMargin = dialogMarginBottom
        }
        showDialogClose.yes {
            mViewBinding.dialogClose.setOnClickListener { dismiss() }
            mViewBinding.dialogClose.visible()
        }

        //标题
        ViewUtils.setVisible(mViewBinding.dialogLoadingTitle, titleTxt.isNoEmpty())
        ViewUtils.setVisible(mViewBinding.titleLine, titleTxt.isNoEmpty())
        ViewUtils.setTextColor(mViewBinding.dialogLoadingTitle, color = AppUtils.getColor(titleTxtColor))
        mViewBinding.dialogLoadingTitle.text = titleTxt
        setTextViewBold(mViewBinding.dialogLoadingTitle)

        //设置标题背景颜色
        mViewBinding.dialogLoadingTitle.getDelegate()
            .setBackgroundColor(AppUtils.getColor(titleBackgroundColor))


        noBottomButtons.no {
            setDefaultData()
            initButtonList()
        }.otherwise {
            mViewBinding.bottomLine.gone()
        }

        //居中容器参数
        val layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, getSelfHeight() - dpDimenPx(R.dimen.dp_50).toInt())
        if (contentTxt != null) {
            val textView = TextView(context)
            textView.setTextColor(AppUtils.getColor(contentColor))
            textView.textSize = 16f
            textView.text = contentTxt
            val dp13 = dp2px(13)
            textView.setPadding(dp13, dp13, dp13, dp13)
            //内容文本居中
            contentCenter.yes {
                layoutParams.gravity = Gravity.CENTER
                textView.gravity = Gravity.CENTER
                mViewBinding.dialogContentLayout.addView(textView, layoutParams)
            }.otherwise {
                positionIsCenter.no {
                    layoutParams.height = dp2px(400)
                    val scrollView = ScrollView(context)
                    scrollView.addView(textView)
                    mViewBinding.dialogContentLayout.addView(scrollView, layoutParams)
                }.otherwise { mViewBinding.dialogContentLayout.addView(textView, layoutParams) }
            }
        } else if (contentLayout != null) {
            contentCenter.yes {
                layoutParams.gravity = Gravity.CENTER
                mViewBinding.dialogContentLayout.addView(contentLayout, layoutParams)
            }.otherwise { mViewBinding.dialogContentLayout.addView(contentLayout, layoutParams) }
        }

    }

    override fun getTheme(): Int = R.style.dialog

    fun setContent(
            text: CharSequence,
            @ColorRes color: Int = R.color.color_323232,
            isCenter: Boolean = true
    ): CommonDialog {
        contentTxt = text
        contentColor = color
        contentCenter = isCenter
        return this
    }

    fun setDialogHeight(height: Int): CommonDialog {
        dialogHeight = height
        return this
    }

    fun setDialogWidth(width: Int): CommonDialog {
        dialogWidth = width
        return this
    }

    fun setDialogMarginBottom(margin: Int): CommonDialog {
        dialogMarginBottom = margin
        return this
    }

    //设置内容区域填充控件
    fun setContent(view: View, isCenter: Boolean = true): CommonDialog {
        contentLayout = view
        contentCenter = isCenter
        return this
    }

    //设置是否居中弹出
    fun setPositionIsCenter(positionIsCenter: Boolean): CommonDialog {
        this.positionIsCenter = positionIsCenter
        return this
    }

    //设置标题
    fun setTitle(
        title: String = "",
        @ColorRes titleColor: Int = R.color.color_323232,
        @ColorRes titleBackgroundColor: Int = R.color.white
    ): CommonDialog {
        titleTxt = title
        titleTxtColor = titleColor
        this.titleBackgroundColor = titleBackgroundColor
        return this
    }

    fun setBottomButtonBackground(@ColorRes color: Int): CommonDialog {
        bottomButtonBackground = color
        return this
    }

    //初始化按钮列表
    private fun initButtonList() {
        mData.isEmpty().yes { return }
        mViewBinding.baseDialogList.layoutManager =
                LayoutManagerUtil.getGridLayoutManager(context, mData.size)
        mViewBinding.baseDialogList.adapter = mAdapter
        mAdapter.setOnItemClickListener { _, _, position ->
            onCommonDialogListener?.onClick(this, position, mAdapter.getItem(position))
            defDismiss.yes { dismiss() }
        }
        mAdapter.setList(mData)
    }

    //设置默认按钮
    private fun setDefaultData(): CommonDialog {
        mData.isEmpty().yes {
            mData.add(DialogButBean("取消", R.color.color_3a3a3a))
            mData.add(DialogButBean("确定", R.color.colorPrimaryDark))
        }
        return this
    }

    //不需要底部的按钮
    fun setNoBottomButtons(): CommonDialog {
        this.noBottomButtons = true
        return this
    }

    //是否显示标题右边的关闭按钮
    fun setShowDialogClose(showDialogClose: Boolean): CommonDialog {
        this.showDialogClose = showDialogClose
        return this
    }

    fun setData(data: List<DialogButBean>): CommonDialog {
        noBottomButtons.no {
            mData.clear()
            mData.addAll(data)
            mAdapter.setList(mData)
        }
        return this
    }

    fun getData(): List<DialogButBean> {
        return mData
    }

    class CommonDialogButtonAdapter(val dialog: CommonDialog) :
            BaseQuickAdapter<DialogButBean, BaseViewHolder>(R.layout.base_item_common_dialog) {
        override fun convert(holder: BaseViewHolder, item: DialogButBean) {
            holder.apply {
                setText(R.id.baseDialogItemTv, item.txt)
                setTextColor(R.id.baseDialogItemTv, AppUtils.getColor(item.color))
                val layout = getView<RoundFrameLayout>(R.id.dialogButtonLayout)
                (data.size == 1).yes {
                    layout.getDelegate().setCornerRadius_BL(dp2px(8))
                    layout.getDelegate().setCornerRadius_BR(dp2px(8))
                }.otherwise {
                    layout.getDelegate().setCornerRadius_BL(item.cornerBL)
                    layout.getDelegate().setCornerRadius_BR(item.cornerBR)
                    layout.getDelegate().setCornerRadius_TL(item.cornerTL)
                    layout.getDelegate().setCornerRadius_TR(item.cornerTR)
                }
                (item.bgColor != 0).yes {
                    layout.getDelegate().setBackgroundColor(AppUtils.getColor(item.bgColor))
                }.otherwise {
                    layout.getDelegate()
                        .setBackgroundColor(AppUtils.getColor(dialog.bottomButtonBackground))
                }
            }
        }
    }

    private var onCommonDialogListener: OnCommonDialogListener? = null

    fun setOnCommonDialogListener(
        onCommonDialogListener: OnCommonDialogListener,
        defDismiss: Boolean = true
    ): CommonDialog {
        this.defDismiss = defDismiss
        this.onCommonDialogListener = onCommonDialogListener
        return this
    }

    interface OnCommonDialogListener {
        fun onClick(dialog: CommonDialog, position: Int, data: DialogButBean)
    }

    override fun show(manager: FragmentManager, tag: String?) {
        super.show(manager, "commonDialog")
    }
}